import algorithm as al
import pandas as pd
import os
import mail_send as ms


class outputClass:
    col_name = ['U0', 'U0∪U1', 'U0∪U2', 'U0∪U3', 'U0∪U4', 'U0∪U5']

    time_cost_df = pd.DataFrame(columns=col_name)
    features_number_df = pd.DataFrame(columns=col_name)
    total_time_cost_1_df = pd.DataFrame(columns=col_name)
    total_time_cost_2_df = pd.DataFrame(columns=col_name)
    total_features_1_df = pd.DataFrame(columns=col_name)
    total_features_2_df = pd.DataFrame(columns=col_name)

    filename = ''

    def reset(self):
        self.time_cost_df = pd.DataFrame(columns=self.col_name)
        self.features_number_df = pd.DataFrame(columns=self.col_name)


def exertLen(x):
    return len(x.P)


def exertStr(x):
    return str(x.P)


def algorithm1(my_data, output_o):
    #   存储消耗时间的列表
    time_list = []

    result = [al.calculateConsAndP(my_data.u0, time_list),
              al.calculateConsAndP(my_data.u0.append(my_data.u1, True), time_list),
              al.calculateConsAndP(my_data.u0.append(my_data.u2, True), time_list),
              al.calculateConsAndP(my_data.u0.append(my_data.u3, True), time_list),
              al.calculateConsAndP(my_data.u0.append(my_data.u4, True), time_list),
              al.calculateConsAndP(my_data.u0.append(my_data.u5, True), time_list)]

    time_list = al.updateList(time_list)

    p_numbers = list(map(exertLen, result))
    p_features = list(map(exertStr, result))

    #   添加数据
    output_o.time_cost_df = output_o.time_cost_df.append(pd.Series(time_list, index=output_o.time_cost_df.columns, name='algorithm1'))
    output_o.total_time_cost_1_df = output_o.total_time_cost_1_df.append(pd.Series(time_list, index=output_o.total_time_cost_1_df.columns,
                                                   name=output_o.filename))
    output_o.features_number_df = output_o.features_number_df.append(
        pd.Series(p_numbers, index=output_o.features_number_df.columns, name='algorithm1'))
    output_o.total_features_1_df = output_o.total_features_1_df.append(pd.Series(p_features, index=output_o.total_features_1_df.columns, name=output_o.filename))
    result[0].time_cost = time_list[0]
    return result[0]


def algorithm2(my_data, half_result, output_o):
    time_list = [half_result.time_cost]

    result = [half_result,
              al.increment(my_data.u0, my_data.u1, half_result, time_list), ]

    result.append(al.increment(result[1].new_df, my_data.u2, result[1], time_list))
    result.append(al.increment(result[2].new_df, my_data.u3, result[2], time_list))
    result.append(al.increment(result[3].new_df, my_data.u4, result[3], time_list))
    result.append(al.increment(result[4].new_df, my_data.u5, result[4], time_list))

    time_list = al.updateList(time_list)

    p_numbers = list(map(exertLen, result))
    p_features = list(map(exertStr, result))

    output_o.time_cost_df = output_o.time_cost_df.append(pd.Series(time_list, index=output_o.time_cost_df.columns, name='algorithm2'))
    output_o.total_time_cost_2_df = output_o.total_time_cost_2_df.append(pd.Series(time_list,
                                                                                   index=output_o.total_time_cost_2_df.columns,
                                                                                   name=output_o.filename))
    output_o.features_number_df = output_o.features_number_df.append(
        pd.Series(p_numbers, index=output_o.features_number_df.columns, name='algorithm2'))
    output_o.total_features_2_df = output_o.total_features_2_df.append(pd.Series(p_features, index=output_o.total_features_2_df.columns, name=output_o.filename))


if __name__ == '__main__':
    # 文件的处理 csv列表的初始化
    output = outputClass()

    for root, dirs, files in os.walk('data'):
        for filename in files:
            output.reset()
            output.filename = filename[:filename.find('.')]
            print(output.filename)
            df = al.dataInput(filename)
            data = al.dataPreprocess(df)
            half_result1 = algorithm1(data, output)
            algorithm2(data, half_result1, output)
            output.time_cost_df.to_csv('./time_cost/'+output.filename+'.csv')
            output.features_number_df.to_csv('./features_number/'+output.filename+'.csv')

    output.total_features_1_df.to_csv('tf1.csv')
    output.total_features_2_df.to_csv('tf2.csv')
    output.total_time_cost_1_df.to_csv('tt1.csv')
    output.total_time_cost_2_df.to_csv('tt2.csv')
    ms.send_my()